import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/home/Home.vue'
import None404 from '@/views/None404.vue'
import Login from '@/views/login/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/wellcome',
    children: [
      {
        path: 'wellcome',
        name: 'wellcome',
        component: () => import('@/views/Wellcome/Wellcome.vue')
      },
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/users-all/users/Users.vue')
      },

      {
        path: 'rights',
        name: 'rights',
        component: () => import('@/views/users-all/users/Users.vue')
      },
      {
        path: 'roles',
        name: 'roles',
        component: () => import('@/views/users-all/users/Users.vue')
      },
      {
        path: 'department',
        name: 'department',
        component: () => import('@/views/department/Department.vue')
      },
      {
        path: 'archives',
        name: 'archives',
        component: () => import('@/views/archives/Archives.vue')
      },
      {
        path: 'devices',
        name: 'devices',
        component: () => import('@/views/devices/Devices.vue')
      },

      {
        path: 'reports',
        name: 'reports',
        component: () => import('@/views/reports/Reports.vue')
      }
    ]
  },
  {
    path: '*',
    component: None404
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫导航判断 token
router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token') || ''

  // 如果存在 token，或者是登录请求，过
  if (token || to.path === '/login') {
    next()
  } else {
    // 否则，转到 login 页
    next({
      path: '/login'
    })
  }
})

router.afterEach(() => {})

export default router
